/**
 */
class Card extends egret.DisplayObjectContainer {
    public static NAME: string = "Card";
    public cardValue: number;     //牌值 例如四张1万中的1张，0x011 
    public cardBg: egret.Bitmap;  //牌背景
    public cardImg: egret.Bitmap; //牌值图片
    public userPos: UserPosition; //方向
    public bUp: boolean = false;  //双击出牌，第一次点击会弹起，第二次点击才出牌
    public upDist: number = 20;   //弹起的距离
    public initPosY: number = 0;  //初始y位置，用于牌的多种动画后，恢复原位时，防止位置错乱
    public childAt: number = 0;    //牌的层次
    private lockSprite: egret.Sprite;

    public constructor() {
        super();
        this.width = 70;
        this.height = 99;
        this.cardBg = new egret.Bitmap();
        this.cardImg = new egret.Bitmap();
        this.addChild(this.cardBg);
        this.addChild(this.cardImg);
        this.touchChildren = false;
        this.touchEnabled = false;

    }

    private createLock() {
        if (this.lockSprite) return;
        this.lockSprite = new egret.Sprite();
        this.lockSprite.graphics.beginFill(0, 0.5);
        this.lockSprite.graphics.drawRoundRect(0, 0, this.width, this.height, 10, 10);
        this.lockSprite.graphics.endFill();
    }

    public lock() {
        this.touchEnabled = false;
        this.createLock();
        this.addChild(this.lockSprite);
    }

    public unLock() {
        this.touchEnabled = true;
        this.lockSprite && this.lockSprite.parent && this.lockSprite.parent.removeChild(this.lockSprite);
    }

	/**
	 * 设置手牌皮肤
	 * @param cardValue 牌值
	 * @dir 牌方向
	 */
    public setHandSkin(cardValue: number, userPos: UserPosition) {
        this.cardValue = cardValue;
        this.userPos = userPos;
        if (userPos == UserPosition.Down) {
            this.cardBg.bitmapData = RES.getRes("card_big_bg1_png");
            this.cardImg.bitmapData = RES.getRes("card_big_" + cardValue + "_png");
            this.scaleX = .88;
            this.scaleY = .88;
        } else if (userPos == UserPosition.R) {
            this.cardBg.bitmapData = RES.getRes("card_left_bg1_png");
            this.cardImg.bitmapData = null;
            this.scaleX = -0.7;
            this.scaleY = 0.7;
        } else if (userPos == UserPosition.Up) {
            this.cardBg.bitmapData = RES.getRes("card_bgup_png");
            this.cardImg.bitmapData = null;
        } else if (userPos == UserPosition.L) {
            this.cardBg.bitmapData = RES.getRes("card_left_bg1_png");
            this.cardImg.bitmapData = null;
            this.scaleX = 0.7;
            this.scaleY = 0.7;
        }
    }

	/**
	 * 设置出牌皮肤
	 * @param cardValue 牌值
	 * @dir 牌方向
	 */
    public setOutSkin(cardValue: number, userPos: UserPosition) {
        this.cardValue = cardValue;
        this.userPos = userPos;
        if (userPos == UserPosition.Down) {
            this.cardBg.bitmapData = RES.getRes("card_midself_bg_png");
            this.cardImg.bitmapData = RES.getRes("card_small_" + cardValue + "_png");
            this.cardImg.x = 13;
            this.cardImg.y = 8;

        } else if (userPos == UserPosition.R) {
            this.cardBg.bitmapData = RES.getRes("card_left_bg0_png");
            this.cardImg.bitmapData = RES.getRes("card_right_" + cardValue + "_png");
            this.cardImg.x = 10;
            this.cardImg.y = 2;

        } else if (userPos == UserPosition.Up) {
            this.cardBg.bitmapData = RES.getRes("card_midself_bg_png");
            this.cardImg.bitmapData = RES.getRes("card_small_" + cardValue + "_png");
            this.cardImg.x = 13;
            this.cardImg.y = 8;

        } else if (userPos == UserPosition.L) {
            this.cardBg.bitmapData = RES.getRes("card_left_bg0_png");
            this.cardImg.bitmapData = RES.getRes("card_left_" + cardValue + "_png");
            this.cardImg.x = 10;
            this.cardImg.y = 2;
        }
        this.scaleX = this.scaleY = 0.58;
    }

	/**
	 * 设置吃碰杠牌皮肤，只有自己手牌吃碰杠牌时，和打出牌皮肤不一致
	 * @param cardValue 牌值
	 * @param userPos 位置
	 */
    public setEatSkin(cardValue: number, userPos: UserPosition) {
        this.cardValue = cardValue;
        this.userPos = userPos;
        if (userPos == UserPosition.Down) {
            this.cardBg.bitmapData = RES.getRes("card_big_bg0_png");
            this.cardImg.bitmapData = RES.getRes("card_big_" + cardValue + "_png");
            this.cardImg.scaleX = 0.8;
            this.cardImg.scaleY = 0.8;
            this.cardImg.y = -8

        } else if (userPos == UserPosition.R) {
            this.cardBg.bitmapData = RES.getRes("card_left_bg0_png");
            this.cardImg.bitmapData = RES.getRes("card_right_" + cardValue + "_png");
            this.cardImg.x = 10;
            this.cardImg.y = 2;
            this.scaleX = 0.7;
            this.scaleY = 0.7;

        } else if (userPos == UserPosition.Up) {
            this.cardBg.bitmapData = RES.getRes("card_midself_bg_png");
            this.cardImg.bitmapData = RES.getRes("card_small_" + cardValue + "_png");
            this.cardImg.x = 13;
            this.cardImg.y = 8;
            this.scaleX = 0.7;
            this.scaleY = 0.7;
        } else if (userPos == UserPosition.L) {
            this.cardBg.bitmapData = RES.getRes("card_left_bg0_png");
            this.cardImg.bitmapData = RES.getRes("card_left_" + cardValue + "_png");
            this.cardImg.x = 10;
            this.cardImg.y = 2;
            this.scaleX = 0.7;
            this.scaleY = 0.7;
        }

    }

    //设置暗杠皮肤
    public setAnGangSkin(cardValue: number, userPos: UserPosition) {
        this.cardValue = cardValue;
        this.userPos = userPos;
        if (userPos == UserPosition.Down) {
            this.cardBg.bitmapData = RES.getRes("card_big_bg2_png");
            this.cardImg.bitmapData = null;
            this.scaleX = 1;
            this.scaleY = 1;
        } else if (userPos == UserPosition.R) {
            this.cardBg.bitmapData = RES.getRes("card_left_bg2_png");
            this.cardImg.bitmapData = null;
            this.scaleX = 0.7;
            this.scaleY = 0.7;
        } else if (userPos == UserPosition.Up) {
            this.cardBg.bitmapData = RES.getRes("card_big_bg2_png");
            this.cardImg.bitmapData = null;
            this.scaleX = 0.7;
            this.scaleY = 0.7;
        } else if (userPos == UserPosition.L) {
            this.cardBg.bitmapData = RES.getRes("card_left_bg2_png");
            this.cardImg.bitmapData = null;
            this.scaleX = 0.7;
            this.scaleY = 0.7;
        }
    }

    /**弹起*/
    public toUp() {
        if (this.bUp == false) {
            this.y -= this.upDist;
            this.bUp = true;
        }
    }

    /**放下*/
    public toDown() {

        if (this.bUp == true) {
            this.y += this.upDist;
            this.bUp = false;
        }
    }

    //回收到对象池
    public recycle() {
        this.scaleX = this.scaleY = 1;
        this.cardBg.bitmapData = this.cardImg.bitmapData = null;
        this.cardImg.scaleX = this.cardImg.scaleY = 1;
        this.cardValue = 0;
        this.userPos = 0;
        this.bUp = false;
        this.childAt = 0;
        this.x = this.y = 0;
        this.cardBg.x = this.cardBg.y = 0;
        this.cardImg.x = this.cardImg.y = 0;
        this.touchEnabled = false;
        this.touchChildren = false;
        // this.lockSprite && this.lockSprite.graphics.clear();
        // this.lockSprite = null;
        this.anchorOffsetX = this.anchorOffsetY = 0;
        this.parent && this.parent.removeChild(this);
        ObjectPool.getPool(Card.NAME).returnObject(this);
    }
}
